package com.ai3d.rfid.common.utils

import android.graphics.Bitmap
import android.text.TextUtils
import android.util.LruCache

/**
 * @ProjectName : Trias
 * @Author : weijinsong
 * @Time : 2022/10/28 14:24
 * @Description : 图片缓存类
 */
object LruCacheMgr {
    private val TAG = LruCacheMgr::class.java.simpleName
    private var sRenderCache: LruCache<String?, Bitmap>? = null
    private var themeCache: ArrayList<String>? = null
    private val LRU_CACHE_SIZE = 60

    fun init() {
        sRenderCache = LruCache(LRU_CACHE_SIZE)
        themeCache = ArrayList()
    }

    fun getCache(cacheKey: String?): Bitmap? {
        return if (null == sRenderCache || TextUtils.isEmpty(cacheKey)) {
            null
        } else {
            sRenderCache!![cacheKey]
        }
    }

    fun putCache(cacheKey: String?, bitmap: Bitmap?) {
        if (TextUtils.isEmpty(cacheKey) || null == bitmap || bitmap.isRecycled || null == sRenderCache) {
            return
        }
        sRenderCache!!.put(cacheKey, bitmap)
    }

    fun hasCache(cacheKey: String?): Boolean {
        return return if (null == sRenderCache || TextUtils.isEmpty(cacheKey)) {
            false
        } else {
            null != sRenderCache!![cacheKey]
        }
    }

    fun clearCache() {
        if (null != sRenderCache) {
            sRenderCache!!.evictAll()
        }
    }

    fun putThemeCache(cacheKey: String) {
        if (TextUtils.isEmpty(cacheKey) || null == themeCache) {
            return
        }
        themeCache?.add(cacheKey!!)
    }

    fun clearThemeCache() {
        themeCache?.forEach { it ->
            sRenderCache?.remove(it)
        }
    }
}